package com.yilin.medical.http;

import java.lang.reflect.Type;
import java.util.Map;

import com.yilin.medical.R;
import com.yilin.medical.activity.BaseActivity;
import com.yilin.medical.lsh.BaseBean;
import com.yilin.medical.lsh.NetHelper;
import com.yilin.medical.lsh.SimpleException;

import android.os.AsyncTask;
import android.support.v4.app.Fragment;

/**
 * 单任务模式，不支持同时执行两个或多个异步任务
 * 
 * classname:AsyTask
 * 
 * @description: author:shujian 2013-9-11 下午4:52:07
 * 
 * @version 1.0.0
 * 
 */
public abstract class AsyTask<T extends BaseBean> extends
		AsyncTask<Map<String, Object>, Void, T> {
	private String mUrl;
	private Type type;
	private BaseActivity mActivity;
	private boolean isShowProgressBar;
	private String progressMsg;
	private Fragment fragment;

	/**
	 * 在activity中使用
	 */
	public AsyTask(BaseActivity activity, String mUrl, Type t) {
		this(activity, mUrl, t, true);
	}

	/**
	 * 在fragment使用
	 */
	public AsyTask(Fragment fragment, String mUrl, Type t) {
		this(fragment, mUrl, t, true);
	}

	/**
	 * 在activity中使用
	 */
	public AsyTask(BaseActivity activity, String mUrl, Type t,
			boolean showProgressBar) {
		this(activity, mUrl, t, showProgressBar, activity
				.getString(R.string.prgress_message));
	}

	/**
	 * 在fragment使用
	 */
	public AsyTask(Fragment fragment, String mUrl, Type t,
			boolean showProgressBar) {
		this((BaseActivity) fragment.getActivity(), mUrl, t, showProgressBar,
				fragment.getActivity().getString(R.string.prgress_message));
		this.fragment = fragment;
	}

	/**
	 * 在fragment使用
	 */
	public AsyTask(Fragment fragment, String mUrl, Type t,
			boolean showProgressBar, String progressMsg) {
		this((BaseActivity) fragment.getActivity(), mUrl, t, showProgressBar,
				progressMsg);
		this.fragment = fragment;
	}

	/**
	 * 在activity中使用
	 */
	public AsyTask(BaseActivity activity, String mUrl, Type t,
			boolean showProgressBar, String progressMsg) {
		super();
		this.mUrl = mUrl;
		this.type = t;
		this.mActivity = activity;
		this.isShowProgressBar = showProgressBar;
		this.progressMsg = progressMsg;
	}

	@Override
	protected T doInBackground(Map<String, Object>... params) {
		T t = null;
		try {
			// t = NetHelper.httpGet(mUrl, params[0], type);
			t = NetHelper.httpPost(mUrl, params[0], type);
		} catch (SimpleException e) {
			e.printStackTrace();
			return (T) e.convertToBean(type);
		}
		return t;
	}

	@Override
	protected void onPreExecute() {
		// TODO Auto-generated method stub
		if (isShowProgressBar) {
			mActivity.showProgressDialog(progressMsg);
		}
		// if (fragment != null) {
		// ((JBase) fragment).setAsyTask(this);
		// }
		super.onPreExecute();
	}

	@Override
	protected void onPostExecute(T result) {
		// TODO Auto-generated method stub
		if (mActivity == null)
			return;
		if (fragment != null)
			// ((JBase) fragment).setAsyTask(null);
			super.onPostExecute(result);
		if (isShowProgressBar) {
			// mActivity.dismissProgressDialog();
		}
		onPost(result);
	}

	protected abstract void onPost(T result);
}
